package com.kun.comic.fragment;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.kun.comic.R;
import com.kun.comic.Reader;
import com.kun.comic.adapter.ComicAdapter;
import com.kun.comic.extra.Ultils;
import com.kun.comic.model.ComicInfo;

public class ListChapFragment extends Fragment {

	private List<ComicInfo> lst = new ArrayList<ComicInfo>();
	private ListView lv;
	private int type;
	private ComicAdapter adapter;

	private final String urlOnepiece = "http://vechai.info/one-piece/";
	private final String urlHKGH = "http://vechai.info/hiep-khach-giang-ho/";
	private final String urlLPBB = "http://vechai.info/Yongbi-the-Invincible/";
	private final String urlNaruto = "http://vechai.info/Naruto/";
	private final String urlOPM = "http://vechai.info/onepunch-man/";
	private final String urlConan = "http://vechai.info/conan/";
	private final String urlFRT = "http://vechai.info/Fairy-Tail-1/";
	private final String urlDoremon = "http://doctruyen14.com/doremon-full-tron-bo-45-tap";
	private final String urlOLV2 = "http://doctruyen14.com/o-long-vien-phan-2-truyen-tranh-sieu-hai-huoc";

	class CallInit extends AsyncTask<Void, Void, String> {

		@Override
		protected String doInBackground(Void... params) {
			init();
			return null;
		};

		@Override
		protected void onPostExecute(String result) {
			Ultils.DimissDialog();
		}

		@Override
		protected void onPreExecute() {
			Ultils.CreateDialog(getActivity());
		};
	}

	public ListChapFragment(int type) {
		this.type = type;
	}

	private void init() {
		try {
			setupBook();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		setHasOptionsMenu(true);
	}

	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		inflater.inflate(R.menu.main, menu);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_main, container,
				false);
		switch (type) {
		case 1:
			getActivity().setTitle("One piece");
			break;
		case 2:
			getActivity().setTitle("HKGH");
			break;
		case 3:
			getActivity().setTitle("Long phi bất bại P2");
			break;
		case 4:
			getActivity().setTitle("Naruto");
			break;
		case 5:
			getActivity().setTitle("Onepunch-Man");
			break;
		case 6:
			getActivity().setTitle("Conan");
			break;
		case 7:
			getActivity().setTitle("Fairy tail");
			break;
		default:
			getActivity().setTitle(getString(R.string.app_name));
			break;
		}

		new CallInit().execute();

		lv = (ListView) rootView.findViewById(R.id.lv);
		adapter = new ComicAdapter(getActivity(), lst);
		lv.setAdapter(adapter);

		lv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Intent intent = new Intent(getActivity(), Reader.class);
				intent.putExtra("link", lst.get(arg2).getLink());
				intent.putExtra("name", lst.get(arg2).getName());
				intent.putExtra("type", type);
				intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				startActivity(intent);
			}
		});
		return rootView;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.orderasc:
			return true;
		case R.id.orderdesc:
			return true;
		case R.id.orderoutside:
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	private void setupBook() throws IOException {
		String url = "";
		switch (type) {
		case 1:
			url = urlOnepiece;
			break;
		case 2:
			url = urlHKGH;
			break;
		case 3:
			url = urlLPBB;
			break;
		case 4:
			url = urlNaruto;
			break;
		case 5:
			url = urlOPM;
			break;
		case 6:
			url = urlConan;
			break;
		case 7:
			url = urlFRT;
			break;
		case 8:
			url = urlDoremon;
			break;
		case 9:
			url = urlOLV2;
			break;
		default:
			break;
		}

		Document doc = Jsoup.connect(url).get();
		Elements links = doc.select("a[href]");
		for (Element element : links) {
			String ahref = element.toString();
			switch (type) {
			case 1:
				if (ahref.contains("http://doctruyen.vechai.info/one-piece")) {
					String chapname = element.text();
					ComicInfo info = new ComicInfo();
					info.setName(chapname);
					info.setLink(element.attr("href"));
					lst.add(info);
				}
				break;
			case 2:
				if (ahref.contains("http://doctruyen.vechai.info/hiep-khach")) {
					String chapname = element.text();
					ComicInfo info = new ComicInfo();
					info.setName(chapname);
					info.setLink(element.attr("href"));
					lst.add(info);
				}
				break;
			case 3:
				if (ahref
						.contains("http://doctruyen.vechai.info/long-phi-bat-bai-phan-2")) {
					String chapname = element.text();
					ComicInfo info = new ComicInfo();
					info.setName(chapname);
					info.setLink(element.attr("href"));
					lst.add(info);
				}
				break;
			case 4:
				if (ahref.contains("http://doctruyen.vechai.info/naruto")) {
					String chapname = element.text();
					ComicInfo info = new ComicInfo();
					info.setName(chapname);
					info.setLink(element.attr("href"));
					lst.add(info);
				}
				break;
			case 5:
				if (ahref.contains("http://doctruyen.vechai.info/onepunch-man")) {
					String chapname = element.text();
					ComicInfo info = new ComicInfo();
					info.setName(chapname);
					info.setLink(element.attr("href"));
					lst.add(info);
				}
				break;
			case 6:
				if (ahref.contains("http://doctruyen.vechai.info/conan")) {
					String chapname = element.text();
					ComicInfo info = new ComicInfo();
					info.setName(chapname);
					info.setLink(element.attr("href"));
					lst.add(info);
				}
				break;
			case 7:
				if (ahref.contains("http://doctruyen.vechai.info/fairy-tail")) {
					String chapname = element.text();
					ComicInfo info = new ComicInfo();
					info.setName(chapname);
					info.setLink(element.attr("href"));
					lst.add(info);
				}
				break;
			case 8:
				if (ahref.contains("http://doctruyen14.com/doremon-bo")) {
					String chapname = element.text();
					ComicInfo info = new ComicInfo();
					info.setName(chapname);
					info.setLink(element.attr("href"));
					lst.add(info);
				}
				break;
			case 9:
				if (ahref.contains("http://doctruyen14.com/o-long-vien")) {
					String chapname = element.text();
					ComicInfo info = new ComicInfo();
					info.setName(chapname);
					info.setLink(element.attr("href"));
					lst.add(info);
				}
				break;
			default:
				break;
			}

		}
		
	}
}